package com.weixin.weixinmanage.action;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.weixin.weixinmanage.entity.Article;
import com.weixin.weixinmanage.entity.DefaultInfo;
import com.weixin.weixinmanage.entity.Matching;
import com.weixin.weixinmanage.entity.MatchingArticle;
import com.weixin.weixinmanage.service.WeixinService;
import com.weixin.weixinmanage.util.CommonUtil;

public class WeixinAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6696916953108937176L;
	
	private Logger log = Logger.getLogger(WeixinAction.class);
	
	private HttpServletRequest request;
	
	private DefaultInfo defaultInfo;
	
	private Matching matching;
	
	private Article article;
	
	private MatchingArticle matchingArticle;
	
	private WeixinService weixinService;
	
	private HashMap<String, Object> queryJson;
	
	private String page = "1";
	
	private String rows = "10";
	
	/**
	 * 添加图文
	 * @return
	 */
	public String addDefaultInfo(){
		queryJson = new HashMap<String, Object>();
		defaultInfo = new DefaultInfo();
		request = ServletActionContext.getRequest();
		try{
			defaultInfo.setCatalogtitle(request.getParameter("catalogTitle"));
			defaultInfo.setTitle(request.getParameter("title"));
			defaultInfo.setDescribes(request.getParameter("describes"));
			defaultInfo.setUrl(request.getParameter("url"));
			defaultInfo.setImageurl(request.getParameter("imageUrl"));
			
			defaultInfo.setTitle1(CommonUtil.StringCheck(request.getParameter("title1")));
			defaultInfo.setDescribes1(CommonUtil.StringCheck(request.getParameter("describes1")));
			defaultInfo.setUrl1(CommonUtil.StringCheck(request.getParameter("url1")));
			defaultInfo.setImageurl1(CommonUtil.StringCheck(request.getParameter("imageUrl1")));
		
			defaultInfo.setTitle2(CommonUtil.StringCheck(request.getParameter("title2")));
			defaultInfo.setDescribes2(CommonUtil.StringCheck(request.getParameter("describes2")));
			defaultInfo.setUrl2(CommonUtil.StringCheck(request.getParameter("url2")));
			defaultInfo.setImageurl2(CommonUtil.StringCheck(request.getParameter("imageUrl2")));
			
			defaultInfo.setTitle3(CommonUtil.StringCheck(request.getParameter("title3")));
			defaultInfo.setDescribes3(CommonUtil.StringCheck(request.getParameter("describes3")));
			defaultInfo.setUrl3(CommonUtil.StringCheck(request.getParameter("url3")));
			defaultInfo.setImageurl3(CommonUtil.StringCheck(request.getParameter("imageUrl3")));
			defaultInfo.setStatus(Integer.parseInt(request.getParameter("status").toString()));
			defaultInfo.setSearchs(Integer.parseInt(request.getParameter("searchs").toString()));
			
			String message = weixinService.addDefaultInfo(defaultInfo);
			queryJson.put("message", message);
		}catch(Exception e){
			queryJson.put("message", "添加失败");
		}
		return "addDefaultInfo";
	}
	
	
	/**
	 * 查询图文
	 * @return
	 */
	public String selectDefaultInfo(){
		queryJson = new HashMap<String, Object>();
		request = ServletActionContext.getRequest();
		defaultInfo = new DefaultInfo();
		try{
			if(request.getParameter("page").toString()!=null){
				page = request.getParameter("page").toString();
				rows = request.getParameter("rows").toString();
				defaultInfo.setRows(Integer.parseInt(rows));
				int start = (Integer.parseInt(page)-1) * Integer.parseInt(rows);
				defaultInfo.setStart(start);
			}
			if(null != request.getParameter("status") && !"2".equals(request.getParameter("status"))){
				defaultInfo.setStatus(Integer.parseInt(request.getParameter("status").toString()));
			}
			if(null != request.getParameter("catalogTitle") && !"".equals(request.getParameter("catalogTitle"))){
				defaultInfo.setCatalogtitle(java.net.URLDecoder.decode(java.net.URLDecoder.decode(request.getParameter("catalogTitle"),"UTF-8"),"UTF-8"));
			}
			if(null != request.getParameter("createdate")&& !"".equals(request.getParameter("createdate"))){
				defaultInfo.setCreatedate(CommonUtil.StringToTime(request.getParameter("createdate")));
			}
			
			int count = weixinService.selectCountDefaultInfo(defaultInfo);
			List<DefaultInfo> resultList = weixinService.selectDefaultInfo(defaultInfo);
			queryJson.put("total", count);
			queryJson.put("rows", resultList);
		}catch(Exception e){
			log.error("", e);
		}
		return "selectDefaultInfo";
	}
	
	
	/**
	 * 修改图文
	 * @return
	 */
	public String updateDefaultInfo(){
		queryJson = new HashMap<String, Object>();
		request = ServletActionContext.getRequest();
		defaultInfo = new DefaultInfo();
		
		try{
			defaultInfo.setCatalogtitle(request.getParameter("catalogTitle"));
			defaultInfo.setTitle(request.getParameter("title"));
			defaultInfo.setDescribes(request.getParameter("describes"));
			defaultInfo.setUrl(request.getParameter("url"));
			defaultInfo.setImageurl(request.getParameter("imageUrl"));
			
			defaultInfo.setTitle1(CommonUtil.StringCheck(request.getParameter("title1")));
			defaultInfo.setDescribes1(CommonUtil.StringCheck(request.getParameter("describes1")));
			defaultInfo.setUrl1(CommonUtil.StringCheck(request.getParameter("url1")));
			defaultInfo.setImageurl1(CommonUtil.StringCheck(request.getParameter("imageUrl1")));
		
			defaultInfo.setTitle2(CommonUtil.StringCheck(request.getParameter("title2")));
			defaultInfo.setDescribes2(CommonUtil.StringCheck(request.getParameter("describes2")));
			defaultInfo.setUrl2(CommonUtil.StringCheck(request.getParameter("url2")));
			defaultInfo.setImageurl2(CommonUtil.StringCheck(request.getParameter("imageUrl2")));
			
			defaultInfo.setTitle3(CommonUtil.StringCheck(request.getParameter("title3")));
			defaultInfo.setDescribes3(CommonUtil.StringCheck(request.getParameter("describes3")));
			defaultInfo.setUrl3(CommonUtil.StringCheck(request.getParameter("url3")));
			defaultInfo.setImageurl3(CommonUtil.StringCheck(request.getParameter("imageUrl3")));
			defaultInfo.setStatus(Integer.parseInt(request.getParameter("status").toString()));
			defaultInfo.setId(Integer.parseInt(request.getParameter("id")));
			
			String message = weixinService.updateDefaultInfo(defaultInfo);
			queryJson.put("message", message);
		}catch(Exception e){
			queryJson.put("message", "修改失败");
		}
		return "updateDefaultInfo";
	}
	
	
	/**
	 * 删除图文
	 * @return
	 */
	public String deleteDefaultInfo(){
		queryJson = new HashMap<String, Object>();
		request = ServletActionContext.getRequest();
		try {
			int id=Integer.parseInt(request.getParameter("did"));
			boolean flag = weixinService.deleteDefaultInfo(id);
			if(flag){
				queryJson.put("msg", "ok");
			}else{
				queryJson.put("message", "删除失败");
			}
		} catch (Exception e) {
			queryJson.put("message", "删除失败");
		} 
		
		return "deleteDefaultInfo";
	}
	
	/**
	 * 查找图文匹配词
	 * @return
	 */
	public String selectMatching(){
		queryJson = new HashMap<String, Object>();
		request = ServletActionContext.getRequest();
		matching = new Matching();
		try{
			if(request.getParameter("page").toString()!=null){
				page = request.getParameter("page").toString();
				rows = request.getParameter("rows").toString();
				matching.setRows(Integer.parseInt(rows));
				int start = (Integer.parseInt(page)-1) * Integer.parseInt(rows);
				matching.setStart(start);
			}
			int defaultinfoid = Integer.parseInt(request.getParameter("defaultinfoid"));
			matching.setDefaultinfoid(defaultinfoid);
			
			int count = weixinService.selectCountMatching(matching);
			List<Matching> resultList = weixinService.selectMatching(matching);
			queryJson.put("total", count);
			queryJson.put("rows", resultList);
			
		}catch(Exception e){
			log.error("", e);
		}
		
		return "selectMatching";
	}
	
	/**
	 * 增加图文匹配词
	 * @return
	 */
	public String addMatching(){
		queryJson = new HashMap<String, Object>();
		request = ServletActionContext.getRequest();
		matching = new Matching();
		try{
			if(null != request.getParameter("word") && !"".equals(request.getParameter("word"))){
				matching.setWord(request.getParameter("word"));
			}
			if(null != request.getParameter("defaultInfoId") && !"".equals(request.getParameter("defaultInfoId"))){
				matching.setDefaultinfoid(Integer.parseInt(request.getParameter("defaultInfoId")));
			}
			String message = weixinService.addMatching(matching);
			queryJson.put("message", message);
		}catch(Exception e){
			queryJson.put("message", "添加失败");
		}
		
		return "addMatching";
	}
	
	/**
	 * 修改图文匹配词
	 * @return
	 */
	public String updateMatching(){
		queryJson = new HashMap<String, Object>();
		request = ServletActionContext.getRequest();
		matching = new Matching();
		try{
			if(null != request.getParameter("word") && !"".equals(request.getParameter("word"))){
				matching.setWord(request.getParameter("word"));
			}
			if(null != request.getParameter("defaultInfoId") && !"".equals(request.getParameter("defaultInfoId"))){
				matching.setDefaultinfoid(Integer.parseInt(request.getParameter("defaultInfoId")));
			}
			if(null != request.getParameter("id") && !"".equals(request.getParameter("id"))){
				matching.setId(Integer.parseInt(request.getParameter("id")));
			}
			String message = weixinService.updateMatching(matching);
			queryJson.put("message", message);
			
		}catch(Exception e){
			queryJson.put("message", "修改失败");
		}
		
		return "updateMatching";
	}
	
	/**
	 * 删除图文匹配词
	 * @return
	 */
	public String deleteMatching(){
		queryJson = new HashMap<String, Object>();
		request = ServletActionContext.getRequest();
		try{
			int id=Integer.parseInt(request.getParameter("matchingid"));
			weixinService.deleteMatching(id);
			queryJson.put("msg", "ok");
		}catch(Exception e){
			queryJson.put("msg", "fail");
		}
		
		return "deleteMatching";
	}
	
	
	/**
	 * 更新缓存
	 * @return
	 */
	public String addMemcacheMatching(){
		queryJson = new HashMap<String, Object>();
		try{
			boolean f = weixinService.addDefaultMemcache();
			if(f){
				log.info("[updateCacheMemory:]"+f);
				queryJson.put("msg", "ok");
			}else{
				log.info("[updateCacheMemory:]"+f);
				queryJson.put("msg", "fail");
			}
		}catch(Exception e){
			queryJson.put("msg", "fail");
			
		}
		return "addMemcacheMatching";
	}
	
	
	/**
	 * 新增文本
	 * @return
	 */
	public String addArticle(){
		queryJson = new HashMap<String, Object>();
		request = ServletActionContext.getRequest();
		article = new Article();
		try{
			article.setCatalogtitle(request.getParameter("catalogTitle"));
			article.setTitle(request.getParameter("title"));
			article.setDescribes(request.getParameter("describes"));
			article.setUrl(request.getParameter("url"));
			article.setStatus(Integer.parseInt(request.getParameter("status").toString()));
			article.setSearchs(Integer.parseInt(request.getParameter("searchs").toString()));
			
			String message = weixinService.addArticle(article);
			queryJson.put("message", message);
		}catch(Exception e){
			queryJson.put("message", "添加失败");
		}
		
		return "addArticle";
	}
	
	
	/**
	 * 查找文本
	 * @return
	 */
	public String selectArticle(){
		queryJson = new HashMap<String, Object>();
		request = ServletActionContext.getRequest();
		article = new Article();
		try{
			if(request.getParameter("page").toString()!=null){
				page = request.getParameter("page").toString();
				rows = request.getParameter("rows").toString();
				article.setRows(Integer.parseInt(rows));
				int start = (Integer.parseInt(page)-1) * Integer.parseInt(rows);
				article.setStart(start);
			}
			if(null != request.getParameter("status") && !"2".equals(request.getParameter("status"))){
				article.setStatus(Integer.parseInt(request.getParameter("status").toString()));
			}
			if(null != request.getParameter("catalogTitle") && !"".equals(request.getParameter("catalogTitle"))){
				article.setCatalogtitle(java.net.URLDecoder.decode(java.net.URLDecoder.decode(request.getParameter("catalogTitle"),"UTF-8"),"UTF-8"));
			}
			if(null != request.getParameter("createdate")&& !"".equals(request.getParameter("createdate"))){
				article.setCreatedate(CommonUtil.StringToTime(request.getParameter("createdate")));
			}
			int count = weixinService.selectCountArticle(article);
			List<Article> resultList = weixinService.selectArticle(article);
			queryJson.put("total", count);
			queryJson.put("rows", resultList);
		}catch(Exception e){
			log.error("", e);
			
		}
		return "selectArticle";
	}
	
	/**
	 * 更新文本
	 * @return
	 */
	public String updateArticle(){
		queryJson = new HashMap<String, Object>();
		request = ServletActionContext.getRequest();
		article = new Article();
		try{
			article.setCatalogtitle(request.getParameter("catalogTitle"));
			article.setTitle(request.getParameter("title"));
			article.setDescribes(request.getParameter("describes"));
			article.setUrl(request.getParameter("url"));
			article.setStatus(Integer.parseInt(request.getParameter("status").toString()));
			article.setId(request.getParameter("id").toString());
			
			String message = weixinService.updateArticle(article);
			queryJson.put("message", message);
		}catch(Exception e){
			queryJson.put("message", "更新失败");
		}
		
		return "updateArticle";
	}
	
	/**
	 * 删除文本
	 * @return
	 */
	public String deleteArticle(){
		queryJson = new HashMap<String, Object>();
		request = ServletActionContext.getRequest();
		try{
			String id = request.getParameter("did");
			boolean flag = weixinService.deleteArticle(id);
			if(flag){
				queryJson.put("msg", "ok");
			}else{
				queryJson.put("message", "删除失败");
			}
		} catch (Exception e) {
			queryJson.put("message", "删除失败");
		} 
		return "deleteArticle";
	}
	
	
	/**
	 * 查找文本匹配词
	 * @return
	 */
	public String selectMatchingArticle(){
		queryJson = new HashMap<String, Object>();
		request = ServletActionContext.getRequest();
		matchingArticle = new MatchingArticle();
		try{
			if(request.getParameter("page").toString()!=null){
				page = request.getParameter("page").toString();
				rows = request.getParameter("rows").toString();
				matchingArticle.setRows(Integer.parseInt(rows));
				int start = (Integer.parseInt(page)-1) * Integer.parseInt(rows);
				matchingArticle.setStart(start);
			}
			String defaultinfoid = request.getParameter("defaultinfoid").toString();
			matchingArticle.setDefaultinfoid(defaultinfoid);
			
			int count = weixinService.selectCountMatchingArticle(matchingArticle);
			List<MatchingArticle> resultList = weixinService.selectMatchingArticle(matchingArticle);
			queryJson.put("total", count);
			queryJson.put("rows", resultList);
			
		}catch(Exception e){
			log.error("", e);
		}
		
		return "selectMatchingArticle";
	}
	
	/**
	 * 增加文本匹配词
	 * @return
	 */
	public String addMatchingArticle(){
		queryJson = new HashMap<String, Object>();
		request = ServletActionContext.getRequest();
		matchingArticle = new MatchingArticle();
		try{
			if(null != request.getParameter("word") && !"".equals(request.getParameter("word"))){
				matchingArticle.setWord(request.getParameter("word"));
			}
			if(null != request.getParameter("defaultInfoId") && !"".equals(request.getParameter("defaultInfoId"))){
				matchingArticle.setDefaultinfoid(request.getParameter("defaultInfoId").toString());
			}
			String message = weixinService.addMatchingArticle(matchingArticle);
			queryJson.put("message", message);
		}catch(Exception e){
			queryJson.put("message", "添加失败");
		}
		return "addMatchingArticle";
	}
	
	
	/**
	 * 修改文本匹配词
	 * @return
	 */
	public String updateMatchingArticle(){
		queryJson = new HashMap<String, Object>();
		request = ServletActionContext.getRequest();
		matchingArticle = new MatchingArticle();
		try{
			if(null != request.getParameter("word") && !"".equals(request.getParameter("word"))){
				matchingArticle.setWord(request.getParameter("word"));
			}
			if(null != request.getParameter("defaultInfoId") && !"".equals(request.getParameter("defaultInfoId"))){
				matchingArticle.setDefaultinfoid(request.getParameter("defaultInfoId").toString());
			}
			if(null != request.getParameter("id") && !"".equals(request.getParameter("id"))){
				matchingArticle.setId(request.getParameter("id").toString());
			}

			String message = weixinService.updateMatchingArticle(matchingArticle);
			queryJson.put("message", message);
			
		}catch(Exception e){
			queryJson.put("message", "修改失败");
		}
		
		return "updateMatchingArticle";
	}
	
	/**
	 * 删除文本匹配词
	 * @return
	 */
	public String deleteMatchingArticle(){
		queryJson = new HashMap<String, Object>();
		request = ServletActionContext.getRequest();
		try{
			String id=request.getParameter("matchingid");
			weixinService.deleteMatchingArticle(id);
			queryJson.put("msg", "ok");
		}catch(Exception e){
			queryJson.put("msg", "fail");
		}
		
		return "deleteMatchingArticle";
	}
	
	/**
	 * 更新缓存
	 * @return
	 */
	public String addMemcacheMatchingArticle(){
		queryJson = new HashMap<String, Object>();
		try{
			boolean f = weixinService.addArticleMemcache();
			if(f){
				log.info("[updateCacheMemory:]"+f);
				queryJson.put("msg", "ok");
			}else{
				log.info("[updateCacheMemory:]"+f);
				queryJson.put("msg", "fail");
			}
		}catch(Exception e){
			queryJson.put("msg", "fail");
			
		}
		return "addMemcacheMatchingArticle";
	}
	
	
	/**
	 * 查找菜单
	 * @return
	 */
	public String selectAllMenus(){
		
		
		return "selectMenu";
	}
	
	
	
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public DefaultInfo getDefaultInfo() {
		return defaultInfo;
	}

	public void setDefaultInfo(DefaultInfo defaultInfo) {
		this.defaultInfo = defaultInfo;
	}

	public HashMap<String, Object> getQueryJson() {
		return queryJson;
	}

	public void setQueryJson(HashMap<String, Object> queryJson) {
		this.queryJson = queryJson;
	}
	
	public WeixinService getWeixinService() {
		return weixinService;
	}

	public void setWeixinService(WeixinService weixinService) {
		this.weixinService = weixinService;
	}
	
	public String getPage() {
		return page;
	}


	public void setPage(String page) {
		this.page = page;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}


	public Matching getMatching() {
		return matching;
	}


	public void setMatching(Matching matching) {
		this.matching = matching;
	}


	public Article getArticle() {
		return article;
	}


	public void setArticle(Article article) {
		this.article = article;
	}


	public MatchingArticle getMatchingArticle() {
		return matchingArticle;
	}


	public void setMatchingArticle(MatchingArticle matchingArticle) {
		this.matchingArticle = matchingArticle;
	}


}
